package com.kyzh.core.pager.gamedetail

import android.app.Activity
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gushenge.atools.util.AView
import com.gushenge.atools.util.setHeight
import com.gushenge.core.base.fragment.BaseFragment
import com.gushenge.core.beans.Deal
import com.gushenge.core.impls.GameImpl
import com.gushenge.core.listeners.ResultListener
import com.kyzh.core.R
import com.kyzh.core.adapters.GameDetailDealAdapter
import com.kyzh.core.uis.recyclerView
import com.kyzh.core.uis.smartRefreshLayout
import com.kyzh.core.utils.ViewUtils.dp
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.scwang.smart.refresh.layout.listener.ScrollBoundaryDecider
import org.jetbrains.anko.backgroundColor
import org.jetbrains.anko.matchParent
import org.jetbrains.anko.support.v4.UI
import org.jetbrains.anko.toast
import org.jetbrains.anko.verticalLayout

class GameDealFragment: BaseFragment(),ResultListener {
    lateinit var recycler: RecyclerView
    lateinit var root: LinearLayout
    lateinit var refresh: SmartRefreshLayout
    lateinit var adapter: GameDetailDealAdapter
    val deals = arrayListOf<Deal>()
    var p=1
    var max=1
    lateinit var context: Activity
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        context = requireActivity()
        return UI {
            root = verticalLayout {
                backgroundColor = Color.WHITE
                refresh = smartRefreshLayout {
                    setEnableRefresh(true)
                    setEnableAutoLoadMore(false)
                    setEnableOverScrollBounce(true)
                    setEnableLoadMoreWhenContentNotFull(false)
                    setEnableNestedScroll(true)

                    setOnLoadMoreListener {
                        if (p>max){
                            toast(getString(R.string.noHaveMore))
                            finishLoadMore()
                            setEnableLoadMore(false)

                        }else{
                            setEnableLoadMore(true)
                            GameImpl.gameDetailDeal(p,this@GameDealFragment)
                        }
                    }
                    setOnRefreshListener {
                        p=1
                        deals.clear()
                        GameImpl.gameDetailDeal(1,this@GameDealFragment)
                    }
                    recycler = recyclerView{
                        layoutManager = object : LinearLayoutManager(context){
                            override fun canScrollHorizontally(): Boolean {
                                return false
                            }
                        }
                    }.lparams(matchParent, matchParent)
                }.lparams(matchParent, matchParent)
            }

        }.view
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        AView.setWidth(root, matchParent)
        AView.setHeight(root, matchParent)
        adapter = GameDetailDealAdapter(R.layout.game_detail_deal_item,deals)
        recycler.adapter = adapter
        refresh.setEnableFooterFollowWhenNoMoreData(true)
        refresh.setScrollBoundaryDecider(object : ScrollBoundaryDecider {
            override fun canRefresh(content: View?): Boolean {
                return recycler.computeVerticalScrollOffset()==0
            }

            override fun canLoadMore(content: View?): Boolean {
                return true
            }
        })
        GameImpl.gameDetailDeal(1,this)

    }

    override fun success(beans: Any, p: Int, max: Int) {
        if (p == 2) {
            deals.clear()
        }
        beans as ArrayList<Deal>
        this.p = p
        this.max = max
        deals.addAll(beans)
        adapter.notifyDataSetChanged()
        refresh.setEnableLoadMore(true)
        refresh.finishRefresh()
        refresh.finishLoadMore()
    }

    override fun error() {
        val inflate = LayoutInflater.from(requireContext()).inflate(R.layout.empty, null)
        inflate.post {
            inflate.setHeight(200.dp)
        }
        adapter.setEmptyView(inflate)
        refresh.setEnableLoadMore(false)
        refresh.finishRefresh()
        refresh.finishLoadMore()
    }
}